﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ConsumeSys.Orm;
using System.Threading.Tasks;

namespace ConsumeSys.Controllers
{
    [IsLogin]
    public class UserController : BaseController
    {
        /// <summary>
        /// 用户业务逻辑
        /// </summary>
        private BLL.UserBLL b_UserBLL = new BLL.UserBLL();

        #region 用户类型

        /// <summary>
        /// 用户类型页面
        /// </summary>
        /// <returns></returns>
        public ActionResult UserTypeList()
        {
            return View();
        }

        /// <summary>
        /// 添加或编辑用户类型
        /// </summary>
        /// <returns></returns>
        public ActionResult AEUserType()
        {
            return View();
        }

        #endregion

        #region 用户

        /// <summary>
        /// 用户列表页面
        /// </summary>
        /// <returns></returns>
        public ActionResult UserList()
        {
            return View();
        }

        /// <summary>
        /// 获取用户json数据
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="rows">页大小</param>
        /// <param name="realName">真实姓名</param>
        /// <returns></returns>
        public async Task<ContentResult> GetUserList(int page, int rows, string realName)
        {
            return await Task.Run(() =>
            {
                Paging<Model.User> _paging = b_UserBLL.GetPagingUser(page, rows, realName);
                Dictionary<string, object> jsonMap = new Dictionary<string, object>();
                jsonMap.Add("total", _paging.RecordCount);
                jsonMap.Add("rows", _paging.DataList);
                string jsonStr = jsonMap.ToJson();
                return Content(jsonStr);
            });
        }

        /// <summary>
        /// 添加或编辑用户页面
        /// </summary>
        /// <param name="user">用户实体</param>
        /// <returns></returns>
        public ActionResult AEUser(Model.User user)
        {
            try
            {
                if (IsPost)
                {
                    if (string.IsNullOrEmpty(user.u_UserName))
                    {
                        throw new Exception("请输入用户名");
                    }
                    if (string.IsNullOrEmpty(user.u_Password))
                    {
                        throw new Exception("请输入密码");
                    }
                    if (string.IsNullOrEmpty(user.u_RealName))
                    {
                        throw new Exception("请输入姓名");
                    }
                    //新增操作
                    int count = b_UserBLL.AddUser(user);
                    if (count > 0)
                    {
                        return Content("success");
                    }
                }
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
            return View();
        }

        #endregion
    }
}
